package com.shop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class Step2 extends Configured implements Tool {
    static class Step2Mapper extends Mapper<Text, DoubleWritable, Text, Text> {
        @Override
        protected void map(Text key, DoubleWritable value, Mapper<Text, DoubleWritable, Text, Text>.Context context) throws IOException, InterruptedException {
            String[] split = key.toString().split(",");
            String v2 = split[0] + ":" + value.toString();
            context.write(new Text(split[1]), new Text(v2));
        }
    }

    static class Step2Reduce extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            StringBuffer stringBuffer = new StringBuffer();
            for (Text value : values) {
                stringBuffer.append(value.toString()).append(",");
            }
            stringBuffer.setLength(stringBuffer.length() - 1);
            context.write(key, new Text(stringBuffer.toString()));
        }
    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path step2input = new Path(
                "hdfs://192.168.10.11:9000/shop1output");
        Path step2output = new Path(
                "hdfs://192.168.10.11:9000/shop2output");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                , conf);
        if (fs.exists(step2output)) fs.delete(step2output, true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("step2");
        job.setJarByClass(this.getClass());

        job.setMapperClass(Step2Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(Step2Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setInputFormatClass(SequenceFileInputFormat.class);
        SequenceFileInputFormat.addInputPath(job, step2input);

//        job.setOutputFormatClass(TextOutputFormat.class);
//        TextOutputFormat.setOutputPath(job, step2output);
        job.setOutputFormatClass(SequenceFileOutputFormat.class);
        SequenceFileOutputFormat.setOutputPath(job, step2output);
        return job.waitForCompletion(true) ? 0 : -1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new Step2(),args));
    }
}
